package cn.sx.dao;

/*
 *@program:sx_emall_ssm
 *@author: sx
 *@desc:购物车相关dao
 *@Time: 2020/9/29  10:44
 */

import cn.sx.entity.Carts;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CartsDao {
    @Select("select count(*)from carts")
    int getCount();
    @Select("SELECT g.cover,g.intro,g.spec,g.price,c.amount FROM goods g JOIN carts c ON c.good_id=g.id")
    List<Carts> selectList();

    @Select("select * from carts where user_id=#{userId}")
    List<Carts>selectListByUseId(@Param("userId")int userId);

    @Select("select ifnull (sum(amount),0) from carts where user_id=#{userId}")
    /**/
    int selectSumAmountByUserId(@Param("userId")int userId);

    @Select("select * from carts where id = #{id}")
    Carts select(@Param("id")int id);

    @Select("select * from carts where user_id=#{userId} and good_id=#{goodId} limit 1")
    Carts selectByUserIdAndGoodId(@Param("userId") int userId, @Param("goodId") int goodId);

    @Delete("delete from carts where user_id=#{goodId}")
    boolean deleteByUserid(int goodId);



    @Insert("INSERT INTO carts(`amount`,`good_id`,`user_id`) VALUE(#{amount},#{goodId},#{userId})")
    @SelectKey(keyProperty = "id",statement = "SELECT LAST_INSERT_ID()",
            before = false,resultType = Integer.class)
    boolean insert(Carts carts);

    @Update("delete from carts where user_id =#{user_id}")
    boolean deleteByUserId(int userId);
    @Update("delete from carts where id=#{id}")
    boolean delete(@Param("id") int id);
    @Update("update carts set amount=amount+#{amount} where id=#{id}")
    public boolean updateAmount(@Param("id") int id, @Param("amount") int amount);
}
